package com.profiloc.ui.screens;

import java.util.Date;
import java.util.Vector;

import net.rim.device.api.i18n.SimpleDateFormat;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Ui;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.CheckboxField;
import net.rim.device.api.ui.component.DateField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.ObjectChoiceField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;

import com.profiloc.model.Profile;
import com.profiloc.ui.custom.CustomButtonField;
import com.profiloc.ui.custom.CustomLabelField;

public class ProfileSettingsScreen extends MainScreen implements FieldChangeListener{

	/*
	 * "profileObject" is the keyword used here 
	 * to create long hashkey for persistant storage
	 */
	
	private DateField startTime;
	private DateField endTime;
	private ObjectChoiceField profilesField;
	private Object [] profiles = {"Loud","Medium","Normal","Silent","Vibrate"};
	SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss aa");
	
	private PersistentObject settings;
	
	private Vector data = new Vector();
	
	private String profileName;
	private double latitude;
	private double longitude;
	private MainMenuScreen mainScreen;
	private int refreshRate;
	private CustomButtonField save;
	private CustomButtonField cancel;
	private CustomButtonField update;
	private CheckboxField acceptCalls;
	private Profile profileObject;
	private Profile editProfileObject;
	private int index;
	private Bitmap icon = Bitmap.getBitmapResource("rsz_3icon.png");
	private CustomLabelField titleBar;
	
	public ProfileSettingsScreen(Profile profile,int index){

		this.index = index;

		titleBar = new CustomLabelField("Profiloc - Profile Settings",Color.WHITE, 0x000000, icon, Field.USE_ALL_WIDTH);

		setTitle(titleBar);
		
		settings= PersistentStore.getPersistentObject(0x4e73ce89227e0b61L);
		
		if(settings.getContents()!=null){
			Vector temp = new Vector();
			temp = (Vector) settings.getContents();
			for(int i=0;i<temp.size();i++){
				data.addElement(temp.elementAt(i));
			}
		}

		editProfileObject = new Profile();
		editProfileObject  = profile;
		editProfileObject.setName(profile.getName());
		editProfileObject.setRefreshTime(profile.getRefreshTime());
		startTime = new DateField("Starting Time :", Long.MAX_VALUE, timeFormat,DateField.TIME);
		startTime.setDate(editProfileObject.getStartTime());
		endTime = new DateField("Ending Time :",Long.MAX_VALUE,timeFormat,DateField.TIME);
		endTime.setDate(editProfileObject.getEndTime());
		add(startTime);
		add(endTime);
		profilesField = new ObjectChoiceField("Profiles :", profiles);
		for(int i=0;i<profilesField.getSize();i++){
			if(editProfileObject.getProfileType().equals(profilesField.getChoice(i))){
				profilesField.setSelectedIndex(i);
				break;
			}
		}
		//add(profilesField);
		
		add(new SeparatorField());
		
		acceptCalls = new CheckboxField("Accept incoming calls", true);
		acceptCalls.setChecked(editProfileObject.getAcceptCalls());
		add(acceptCalls);

	
		HorizontalFieldManager buttonManager = new HorizontalFieldManager(Field.FIELD_LEFT);

		update = new CustomButtonField("Update",0xFFCC00);
		update.setChangeListener(this);
		cancel = new CustomButtonField("Cancel",0xFFCC00);
		cancel.setChangeListener(this);

		buttonManager.add(update);
		buttonManager.add(cancel);
		
		add(buttonManager);
	}
	
	public ProfileSettingsScreen(double latitude,double longitude,String profileName,MainMenuScreen mainScreen,int refresh) {
		
		titleBar = new CustomLabelField("Profiloc - Profile Settings",Color.WHITE, 0x000000, icon, Field.USE_ALL_WIDTH);

		setTitle(titleBar);
		
		settings= PersistentStore.getPersistentObject(0x4e73ce89227e0b61L);
		this.mainScreen = mainScreen;
		this.latitude = latitude;
		this.longitude = longitude;
		this.profileName = profileName;
		this.refreshRate =refresh;
		// TODO Auto-generated constructor stub
		startTime = new DateField("Starting Time :", Long.MAX_VALUE, timeFormat,DateField.TIME);
		endTime = new DateField("Ending Time :",Long.MAX_VALUE,timeFormat,DateField.TIME);
		add(startTime);
		add(endTime);		
		profilesField = new ObjectChoiceField("Profiles :", profiles);
		//add(profilesField);
		
		add(new SeparatorField());

		acceptCalls = new CheckboxField("Accept incoming calls", true);
		add(acceptCalls);


		add(new SeparatorField());
		
		HorizontalFieldManager buttonManager = new HorizontalFieldManager(Field.FIELD_LEFT);

		save = new CustomButtonField("Save",0xFFCC00);
		save.setChangeListener(this);
		cancel = new CustomButtonField("Cancel",0xFFCC00);
		cancel.setChangeListener(this);

		buttonManager.add(save);
		buttonManager.add(cancel);
		
		add(buttonManager);
	}


	public void fieldChanged(Field field, int context) {
		// TODO Auto-generated method stub
		if(field == save){
			
			mainScreen = new MainMenuScreen();
			profileObject = new Profile();
			profileObject.setStartTime(new Date(startTime.getDate()).getTime());
			profileObject.setEndTime(new Date(endTime.getDate()).getTime());
			profileObject.setName(profileName);
			profileObject.setLatitude(latitude);
			profileObject.setLongitude(longitude);
			profileObject.setProfileType((String)profilesField.getChoice(profilesField.getSelectedIndex()));
			profileObject.setAcceptCalls(acceptCalls.getChecked() ? true : false);
			profileObject.setRefreshTime(refreshRate);
			
			if(settings.getContents()!=null){
				Vector temp = new Vector();
				temp = (Vector) settings.getContents();
				
				for(int i=0;i<temp.size();i++){
					data.addElement(temp.elementAt(i));
				}
			}
			data.addElement(profileObject);
			
			synchronized (settings) {
				
				settings.setContents(data);
				settings.commit();
			}
			
			int result = Dialog.ask(Dialog.D_OK_CANCEL,"Your profile \""+ profileObject.getName() +"\" succesfully saved.Press OK to approve,Cancel to return");
			if(result == Dialog.OK){
				
				//UiApplication.getUiApplication().pushScreen(mainScreen);
				UiApplication.getUiApplication().popScreen(this);
				UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
			}else{
				
				startTime.setFocus();
			}
		}
		
		if(field == update){

			editProfileObject.setStartTime(new Date(startTime.getDate()).getTime());
			editProfileObject.setEndTime(new Date(endTime.getDate()).getTime());
			editProfileObject.setLatitude(latitude);
			editProfileObject.setLongitude(longitude);
			editProfileObject.setProfileType((String)profilesField.getChoice(profilesField.getSelectedIndex()));
			editProfileObject.setAcceptCalls(acceptCalls.getChecked() ? true : false);
			editProfileObject.setRefreshTime(refreshRate);		
			
			data.removeElementAt(index);
			data.addElement(editProfileObject);
			
			synchronized (settings) {
			
				settings.setContents(data);
				settings.commit();
			}
			UiApplication.getUiApplication().popScreen(this);
		}
		if(field == cancel){
			
			UiApplication.getUiApplication().popScreen(this);
		}
	}
}
